package com.bjgzy.courselive.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.bjgzy.courselive.mvp.ui.fragment.CourseIntroductionFragment;
import com.bjgzy.courselive.mvp.ui.fragment.CourseListFragment;
import com.bjgzy.module_couselive.R;
import com.bjgzy.module_couselive.R2;
import com.blankj.utilcode.util.ObjectUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.adapter.HomeFragmentAdapter;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.mvp.IView;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-7-31 14:10:27
 */
@Route(path = RouterHub.COURSELIVE_COURSEDETAILSACTIVITY)
public class CourseDetailsActivity extends BaseActivity implements IView {
    @BindView(R2.id.iv_top)
    ImageView ivTop;
    @BindView(R2.id.tabs)
    TabLayout tabs;
    @BindView(R2.id.viewpager)
    ViewPager viewpager;
    @BindView(R2.id.btn_submit)
    QMUIRoundButton btnSubmit;

    @Autowired(name = Constants.ROUTER_INTEGER)
    int internalId;

    List<Fragment> mFragments;

    @Autowired(name = Constants.ROUTER_STRING)
    String originate;
    @Autowired(name = Constants.ROUTER_IMG)
    String img;

    String[] mTitles = new String[]{
            "介绍", "目录"//, "评价"
    };


    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        //如找不到该类,请编译一下项目
    }


    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        MobclickAgent.onEvent(getActivity(), "ttw_course_detail");
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.courselive_activity_details;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        if (ObjectUtils.isNotEmpty(originate)) {
            ivTop.setVisibility(View.GONE);
            btnSubmit.setVisibility(View.GONE);
        } else {
            Glide.with(this)
                    .load(img)
                    .apply(RequestOptions.centerInsideTransform()                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   .diskCacheStrategy(DiskCacheStrategy.ALL))
                    .into(ivTop);
        }

        mFragments = new ArrayList<>();
        CourseIntroductionFragment instance = CourseIntroductionFragment.newInstance();
        instance.setData(internalId);
        mFragments.add(instance);
        CourseListFragment listFragment = CourseListFragment.newInstance();
        listFragment.setData(internalId);
        mFragments.add(listFragment);
//        CourseCommentFragment commentFragment = CourseCommentFragment.newInstance();
//        commentFragment.setData(internalId);
//        mFragments.add(commentFragment);
        // 第二步：为ViewPager设置适配器
        HomeFragmentAdapter adapter = new HomeFragmentAdapter(getSupportFragmentManager(), Arrays.asList(mTitles), mFragments);
        viewpager.setAdapter(adapter);
        viewpager.setOffscreenPageLimit(mTitles.length);
        // 第三步：将ViewPager与TableLayout 绑定在一起
        tabs.setupWithViewPager(viewpager);
//        if (ObjectUtils.isNotEmpty(originate)) {
        viewpager.setCurrentItem(1);
//        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    public Activity getActivity() {
        return this;
    }


    @OnClick(R2.id.btn_submit)
    public void onViewClicked() {
    }

    public void setJoinGone() {
        btnSubmit.setVisibility(View.GONE);
    }
}
